package com.lqian.demo02.part4.dao;

import com.lqian.demo02.utils.CollectionManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author：Melody_LQ
 * @time：2022/7/14 1:07 下午
 */
public class BankDao {


    /**
     * 转款方：转款
     * @param name 转款方姓名
     * @param money 转款金额
     * @return
     */
    public int transfer(String name,Float money) throws SQLException {
        Connection connection = CollectionManager.getConnection();
        String sql = "update account set money=money - ? where name = ?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setFloat(1,money);
        ps.setString(2,name);

        int resultRows = ps.executeUpdate();
        return  resultRows;
    }

    /**
     * 收款方：收款
     * @param name 收款人姓名
     * @param money 收款金额
     * @return
     * @throws SQLException
     */
    public int receiver(String name,Float money) throws SQLException {
        Connection connection = CollectionManager.getConnection();
        String sql = "update account set money=money + ? where name = ?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setFloat(1,money);
        ps.setString(2,name);
        int resultRows = ps.executeUpdate();
        return  resultRows;
    }
}
